package com.dsunsoft.rtu.web;

import com.dsunsoft.rtu.common.constant.CommonConstants;
import com.dsunsoft.rtu.common.entity.RestCode;
import com.dsunsoft.rtu.common.entity.RestResult;
import com.dsunsoft.rtu.common.util.CommonUtils;
import com.dsunsoft.rtu.core.base.BaseRestController;
import com.dsunsoft.rtu.dto.SysRoleDto;
import com.dsunsoft.rtu.entity.SysRole;
import com.dsunsoft.rtu.query.SysMenuQuery;
import com.dsunsoft.rtu.query.SysRoleQuery;
import com.dsunsoft.rtu.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/sysRole")
public class SysRoleController extends BaseRestController<SysRole, SysRoleQuery> {

    @Autowired
    private SysRoleService sysRoleService;

    @Override
    @PostMapping
    public RestResult<String> add(@RequestBody @Validated SysRole entity) {
        entity.setRoleId(CommonUtils.getUuid());
        boolean bl = sysRoleService.insert(entity);
        if (!bl) {
            return new RestResult<String>().failure("保存失败");
        }
        return new RestResult<>(RestCode.SUCCESS);
    }

    @PostMapping(value = "/getRoleMenu")
    public RestResult<List<String>> getRoleMenu(@RequestBody SysMenuQuery query) {
        List<String> list = sysRoleService.getMenuByRoleId(query.getRoleId());
        return new RestResult<>(RestCode.SUCCESS, list);
    }

    @PostMapping("/saveRoleMenu")
    public RestResult<Boolean> roleMenu(@RequestBody @Validated SysRoleDto dto) {
        boolean bl = sysRoleService.saveRoleMenu(dto);
        return new RestResult<>(RestCode.SUCCESS, bl);
    }
}
